<?php

namespace App\Http\Controllers\admin;
use Illuminate\Session;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Admin;
use App\Models\Store;
class AdminController extends Controller
{
    //
    public function index()
    {
        $data=session('adminlist');
        if(empty($data['pic']))
        {
            $data['pic']='default.jpg';
        }
//        dd($data);
       return view('admin.index',['list'=>$data]);

    }
    //显示登录页面
    public function adminlogin()
    {
        return view('admin.login');
    }
    public function indexStore()
    {
        $data=session('storelist');
        if(empty($data['logo']))
        {
            $data['logo']='default.jpg';
        }
        return view('admin.indexStore',['list'=>$data]);
    }



    //查询指定名字的admin用户名
    public function chkadminuname(Request $request)
    {
        $uname=$request->input('uname');
//        return json_encode($uname);
        $admin=new Admin();
        $list=$admin->adminGet($uname);
        if(empty($list))
        {
            return null;
        }
        else{
            return $list;
        }
    }
    public function chkadminlogin(Request $request)
    {
        $uname=$request->input('uname');
        $passwd=md5($request->input('upassword'));
        $admin=new Admin();
        $list=$admin->where('username',$uname)->first();
        if($passwd == $list['password'])
        {
            //存session值
            unset($list['password']);
            session(['adminlist' => $list]);
            return json_encode(true);
        }else{
          return json_encode(false);
        }


    }
    //显示商家登陆界面
    public function storelogin()
    {
        return view('admin.storelogin');
    }

    //查询指定名字的商家
    public function chkstoreuname($name)
    {

    }

    public function chkstorelogin(Request $request)
    {
        $uname=$request->input('uname');
        $passwd=md5($request->input('upassword'));
        $store=new Store();
        $list=$store->where('username',$uname)->first();
        if($passwd == $list['password'])
        {
            unset($list['password']);
            session(['storelist' => $list]);
            return json_encode(true);
        }else{
            return json_encode(false);
        }
    }



}
